package com.sky.loveshop.entity;


import com.github.pagehelper.PageInfo;
import com.sky.loveshop.entity.common.PageResult;

/**
 * @author xcy
 * @version V1.0
 * @description
 * @date 2022/8/13 23:42
 */
public class Result<T> {

    private int code;
    private String message;
    private T content;

    public Result() {

    }

    public Result(int code, String message, T content) {
        this.code = code;
        this.message = message;
        this.content = content;
    }

    public static <T> Result<T> response(int code, String message) {
        return response(code, message, null);
    }

    public static <T> Result<T> response(int code, String message, T content) {
        Result responseDTO = new Result();
        responseDTO.setCode(code);
        responseDTO.setContent(content);
        responseDTO.setMessage(message);
        return responseDTO;
    }


    public static <T> Result<T> success() {
        return Result.response(1, "success", null);
    }

    public static <T> Result<T> success(T content) {
        return Result.response(1, "success", content);
    }


    public static <T> Result<T> ofError(int code, String message, T content) {
        return Result.response(code, message, content);
    }

    public static <T> Result<T> ofError(int code, String message) {
        return Result.response(code, message, null);
    }

    public static <T> Result<T> ofError(String message) {
        return Result.response(1002, message, null);
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getContent() {
        return content;
    }

    public void setContent(T content) {
        this.content = content;
    }


    public static <T, S> Result<PageResult<S>> page(T data) {
        PageResult result = new PageResult();
        com.github.pagehelper.PageInfo<?> page = (PageInfo<?>) data;
        result.setPageNum(page.getPageNum());
        result.setPages(page.getPages());
        result.setPageSize(page.getPageSize());
        result.setTotal(page.getTotal());
        result.setList(page.getList());
        return success(result);
    }

}